using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using GoogleEarthClasses.Global;
using GoogleEarthClasses.Data;
using GoogleEarthClasses.Entities;
namespace GoogleEarthClasses.Logic
{
    public class LPoint
    {
        #region Query
        /// <summary>
        /// connect to the server and download the points between dateStart and dateEnd then create a Point list
        /// </summary>
        /// <param name="dStart"></param>
        /// <param name="dEnd"></param>
        /// <returns></returns>
        public static List<EPoint> SelectAll(DateTime dStart, DateTime dEnd)
        {
            DataTable _Datatable = DPoint.SelectAll(dStart, dEnd);
            return SelectAllServer(_Datatable);
        }
        /// <summary>
        /// recive a datatable and return a Epoint list
        /// </summary>
        /// <param name="_Datatable"></param>
        /// <returns></returns>
        public static List<EPoint> SelectAllServer(DataTable _Datatable)
        {
            List<EPoint> _Points = new List<EPoint>();
            EPoint _Point = new EPoint();
            List<EImage> _Images = new List<EImage>();
            EImage _Image;
            Int32 _PrevLocationId = 0;
            foreach (DataRow dr in _Datatable.Select())
            {
                if (_PrevLocationId != ConvertHelper.ToInt32(dr["LocationId"]))
                {
                    if (_PrevLocationId != 0)
                    {
                        _Point.Images = _Images;
                        _Point.HaveImages = _Images.Count;
                        _Points.Add(_Point);

                    }
                    _PrevLocationId = ConvertHelper.ToInt32(dr["LocationId"]);
                    _Point = new EPoint();
                    _Images = new List<EImage>();

                    _Point.LocationId = _PrevLocationId;
                    _Point.Altitude = double.Parse(ConvertHelper.Tostring(dr["Altitude"]));
                    _Point.Latitude = double.Parse(ConvertHelper.Tostring(dr["Latitude"]));
                    _Point.Longitude = double.Parse(ConvertHelper.Tostring(dr["Longitude"]));
                    _Point.Description = ConvertHelper.Tostring(dr["Description"]);
                    _Point.Angle = double.Parse(ConvertHelper.Tostring(dr["Angle"]));
                    _Point.Satellites = ConvertHelper.ToInt32(dr["Satellites"]);
                    _Point.SatelliteTime = ConvertHelper.ToDateTime(dr["SatelliteTime"]);
                    _Point.Color = ConvertHelper.Tostring(dr["Color"]);
                    _Point.ParentPoint = new EPoint(ConvertHelper.ToInt32(dr["ParentPositionId"]));
                    _Point.Icon = ConvertHelper.Tostring(dr["ParentPositionId"]);
                }
                if (ConvertHelper.ToInt32(dr["ImageId"]) != Int32.MinValue)
                {
                    _Image = new EImage(ConvertHelper.ToInt32(dr["ImageId"]));
                    _Images.Add(_Image);
                }
            }
            _Point.HaveImages = _Images.Count;
            _Point.Images = _Images;
            _Points.Add(_Point);
            return _Points;
        }
        public static List<EPoint> SelectAllSDF(DataTable _Datatable, string PathImages)
        {
            List<EPoint> _Points = new List<EPoint>();
            EPoint _Point = new EPoint();
            foreach (DataRow dr in _Datatable.Select())
            {
                _Point = new EPoint();
                _Point.LocationId = ConvertHelper.ToInt32(dr["LocationId"]);
                _Point.Images = LImage.FindImagesByPosition(_Point.LocationId.ToString(), PathImages);
                _Point.Altitude = double.Parse(ConvertHelper.Tostring(dr["Altitude"]));
                _Point.Latitude = double.Parse(ConvertHelper.Tostring(dr["Latitude"]));
                _Point.Longitude = double.Parse(ConvertHelper.Tostring(dr["Longitude"]));
                _Point.Description = ConvertHelper.Tostring(dr["Description"]);
                _Point.Angle = double.Parse(ConvertHelper.Tostring(dr["Angle"]));
                _Point.Satellites = ConvertHelper.ToInt32(dr["Satellites"]);
                _Point.SatelliteTime = ConvertHelper.ToDateTime(dr["SatelliteTime"]);
                _Point.HaveImages = ConvertHelper.ToInt32(dr["CountImages"]);
                _Point.InsertedPosition = ConvertHelper.ToInt32(dr["UploadedPointId"]);
                _Point.Color = ConvertHelper.Tostring(dr["Color"]);
                _Points.Add(_Point);
            }
            return _Points;
        }
        public static DataTable SelectAllPoints()
        {
            return DPoint.SelectAllPoints();
        }
        public static DataTable ServerSelectAllPoints()
        {
            return DPoint.ServerSelectAllPoints();
        }
        #endregion

        #region INSERT & DELETE
        /*
        public static Int32 InsertAllPoints(double longitude, double latitude, double altitude, string description, DateTime dTime, int nSatellites, double Angle, string Color, SqlTransaction tr)
        {
            return DPoint.Insert(longitude, latitude, altitude, description, dTime, nSatellites,Angle,Color, tr);
        }
        public static Int32 InsertAllPoints(double longitude, double latitude, double altitude, string description, DateTime dTime, int nSatellites, double Angle, string Color)
        {
            SqlTransaction tr = null;
            SqlConnection con = null;
            try
            {
                con = DBHelper.GetConnection();
                tr = con.BeginTransaction();
                return InsertAllPoints(longitude, latitude, altitude, description, dTime, nSatellites, Angle, Color, tr);
            }
            catch (Exception)
            {
                throw;
            }
        }
         */ 
        public static void InsertPositionImages(Int32 IdPosition, string Name, byte[] ImageBytes, SqlTransaction tr )
        {
            DPoint.InsertPositionImages(IdPosition, Name, ImageBytes);
        }

        public static void InsertPositionImages(Int32 IdPosition, string Name, byte[] ImageBytes)
        {
            try
            {
                DPoint.InsertPositionImages(IdPosition, Name, ImageBytes);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>THIS METHOD IS USED TO SET THE PARENT OF A SPECIFIC POLE. </summary>
        public static void SetParent(EPoint Point)
        {
            DPoint.SetParent(Point.LocationId, Point.ParentPoint.LocationId);
        }
  
        public static void DeleteLocalData()
        {
            DPoint.DeleteLocalData();
        }
        #endregion

    }
}
